package com.rd.student.ruanjian.class4.lichunquan.Day0728;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Demo06 {
    public static void main(String[] args) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("StudentInformation.txt"));
        String str =
                "郑成祥、王博、孙文卓、郑睿娴、王高博、李春泉、" +
                        "张睿、王星凯、柏云耀、高艺铭、周子贺、杜航、" +
                        "高晨曦、张思韬、孙昊、方周、郭佳宝、姜万里、李宏鸣、" +
                        "薛志鑫、王海鑫、夏银银、吴俊豪、王向阳、徐铭泽、" +
                        "张世煊、才雨鑫、孙国伟、秦嘉林、王岩博、石睿、杜雨坤、" +
                        "黄奕鸣、金洪宇、李鑫、王宇芳、苏岩";
        String[] strArr = str.split("、");
        StringBuilder stringBuilder = new StringBuilder();
        ArrayList<Student> arr = new ArrayList<>();

        for (int i = 0; i < strArr.length; i++) {
            Student s = new Student(strArr[i], "正常");
            arr.add(s);
            stringBuilder.append(s + "\n");
        }
        oos.writeObject(arr);
        Random_Student rs = new Random_Student();
        while(true){
            System.out.println("输入要随机查询学生个数");
            rs.method(new Scanner(System.in).nextInt());
        }

    }
}

//创建随机器
class Random_Student {

    public void method(int n) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("random.txt"));
        ObjectInputStream ois = new ObjectInputStream(
                new FileInputStream("StudentInformation.txt"));
        ObjectInputStream oisRan = new ObjectInputStream(
                new FileInputStream("random.txt"));
        ArrayList<Student> arr;
        ArrayList<Student> arr_R;
        ArrayList<Integer> arri = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        //获取所有学生信息
        arr = (ArrayList<Student>)ois.readObject();
        int number = 0;
        //随机产生数据
        while (number < n) {
            int i = new Random().nextInt(arr.size());
            int j = 0;
            //防重
            while (j < arri.size()) {
                while (i == arri.get(j)) {
                    i = new Random().nextInt(arr.size());
                }
                j++;
            }
            number++;
            j = 0;
            //获取列表下标保存用于防止重复
            arri.add(i);
            String str = arr.get(i).toString();
            stringBuilder.append(str+"\n");
        }
        oos.writeObject(stringBuilder);
        System.out.println(oisRan.readObject());
    }
}

class Student implements Serializable {
    String name;
    String desc;

    public Student(String name, String desc) {
        this.name = name;
        this.desc = desc;

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}